<template>
  <div class="container">
    <input type="text" placeholder="请输入要发送的内容" v-model="txt" id="ipt" />
    <button @click="show" id="btn">发送</button>
    <button @click="close" id="closeBtn">退出群聊</button>
    <button @click="initWebSocket">重新连接</button>
    <p>群聊内容如下：</p>
  </div>
</template>
<script>
export default {
  name: "two",
  data() {
    return {
      ws: null,
      url: "localhost:3333",
      txt: "",
      list: [], //聊天记录的数组
    };
  },

  mounted() {
    // 初始化
    this.initWebSocket();
    this.$once("hook:destroyed", () => {
      this.ws.close();
    });
  },
  methods: {
    //滚动条到底部
    scrollBottm() {
      let el = this.$refs["container"];
      el.scrollTop = el.scrollHeight;
    },
    //发送聊天信息
    show() {
      let _this = this;
      if (!_this.txt) {
        return;
      }
      _this.websocketsend(_this.txt); //调用WebSocket send()发送信息的方法
      _this.txt = "";
      // setTimeout(() => {
      //   _this.scrollBottm();
      // }, 500);
    },
    close() {
      this.ws.close();
    },
    //进入页面创建websocket连接
    initWebSocket() {
      let _this = this;
      //判断页面有没有存在websocket连接
      if (window.WebSocket) {
        _this.ws = new WebSocket("ws://" + this.url);
        _this.ws.onopen = this.websocketonopen;
        _this.ws.onclose = this.websocketclose;
        _this.ws.onerror = this.websocketonerror;
        _this.ws.onmessage = this.websocketonmessage;
      }
    },
    websocketonopen() {
      console.log("连接成功");
    },
    websocketonerror() {
      console.log("连接失败");
      this.initWebSocket();
    },
    websocketsend(Data) {
      // 数据发送
      this.ws.send(Data);
    },
    websocketclose(e) {
      // 关闭
      console.log("断开连接", e);
    },
    websocketonmessage(e) {
      console.log('e', e)
      // 数据接收
      this.list = JSON.parse(e.data);
      console.log(this.list)
    },
  },
};
</script>
<style lang="less" scoped>
input {
  width: 300px;
  line-height: 30px;
  outline: none;
  padding: 5px 10px;
  font-size: 14px;
  border: 1px solid #eee;
  transition: all 0.35s;
  border-radius: 5px;
}

input:focus {
  color: #333;
  /* border: 0; */
  border: 1px solid #495488;
}
button {
  border: 0;
  background: #05bf44;
  color: #fff;

  padding: 10px 28px;
  border-radius: 8px;
  box-sizing: border-box;
  cursor: pointer;
  outline: none;
  font-size: 14px;
  transition: all 0.35s;
}
button:hover {
  background: #00ab3a;
}
#closeBtn {
  padding: 10px 12px;
  background: tomato;
}
#closeBtn:hover {
  background: #de452a;
}
#rst {
  width: 450px;
  height: 450px;
  padding: 20px;
  box-sizing: border-box;
  margin-bottom: 20px;
  border: 1px solid #f1f1f1;
  background-color: #fdfdfd;
  overflow: auto;
}
span.timer {
  display: inline-block;
  border-radius: 10px;
  margin: 5px 0;
  color: #fff;
  background: #e6e6e6;
  padding: 3px 5px 3px;
}
#rst p,
#rst p.default {
  color: #d6d6d6;
  text-align: center;
  font-size: 12px;
}
#rst p.leave {
  color: #ddd;
}

#rst p.speek {
  color: #333;
  font-size: 14px;
}
#rst p.speek .timer {
  font-size: 12px;
  text-align: center;
}
#rst p.speek .msg {
  display: block;
  text-align: left;
}
</style>